<?php

namespace app\controller\v1\admin;

use app\BaseController;
use app\common\Send;
use app\service\ArticleService;
use app\validate\Article as ArticleValidate;
use think\App;
use think\Request;

class Article extends BaseController
{
    use Send;
    private $articleService;

    public function __construct(App $app,ArticleService $articleService)
    {
        parent::__construct($app);
        $this->articleService = $articleService;
    }

    /**
     * 获取文章列表
     */
    public function index(Request $request)
    {
        $params = $request->param();
        $result = $this->articleService->list($params);
        return self::success($result);
    }

    /**
     * 保存新建的文章
     */
    public function save(Request $request)
    {
        $params = $request->param();
        (new ArticleValidate)->goCheck($params);
        $result = $this->articleService->save($params);
        return self::success($result);
    }

    /**
     * 获取指定文章
     */
    public function read($id)
    {
        $result = $this->articleService->detail($id);
        return self::success($result);
    }

    /**
     * 保存更新的文章
     */
    public function update(Request $request, $id)
    {
        $params = $request->param();
        (new ArticleValidate)->goCheck($params);
        $result = $this->articleService->update($id, $params);
        return self::success($result);
    }

    /**
     * 删除指定文章
     */
    public function delete($id)
    {
        $result = $this->articleService->delete($id);
        return self::success($result);
    }
}
